<?php
class Codetail extends AppModel {
	var $name = 'Codetail';
	var $primaryKey = 'ComID';
	var $displayField = 'ComName';
	//var $actsAs = array('FileUpload.FileUpload');
	var $validate = array(		
		'ComName' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'CoName cannot be left empty',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),'url_title' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'URL title cannot be left empty',
				//'allowEmpty' => false,
				//'required' => false,
				'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),'isUnique' => array(
				'rule' => array('isUnique'),
				'message' => 'URL title is already exists.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		)
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $hasMany = array(
		'Coevent' => array(
			'className' => 'Coevent',
			'foreignKey' => 'ComID',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => 'Coevent.EventDate DESC',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Codexec' => array(
			'className' => 'Codexec',
			'foreignKey' => 'ComID',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => 'Codexec.Name ASC',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Codtran' => array(
			'className' => 'Codtran',
			'foreignKey' => 'ComID',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => 'Codtran.Date DESC',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	function import($filename) {
 		$error=false;
		//echo $filename;
		// open the file
 		$handle = fopen($filename, "r");
 		
 		// read the 1st row as headings
 		$header = fgetcsv($handle);
 		
		// create a message container
		$return = array(
			'messages' => array(),
			'errors' => array(),
		);
		$i=0;
 		// read each data row in the file
 		while (($row = fgetcsv($handle)) !== FALSE) {
 			$i++;
 			$data = array();

 			// for each header field 
 			foreach ($header as $k=>$head) {
 				// get the data field from Model.field
				//echo $head;exit;
 				if (strpos($head,'.')!==false) {
	 				$h = explode('.',$head);
	 				$data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';
				}
 				// get the data field from field
				else {
	 				$data['Codetail'][$head]=(isset($row[$k])) ? $row[$k] : '';
				}
 			}
			// see if we have an id 			
 			$id = isset($data['Codetail']['ComID']) ? $data['Codetail']['ComID'] : 0;
 			/*if(isset($data['Codetail']['delistDate'])){
				$delistDate=explode("/",$data['Codetail']['delistDate']);
				$data['Codetail']['delistDate']=$delistDate['2']."-".$delistDate['0']."".-$delistDate['1'];
			}*/
                        
            //print_r($data);exit;
			// we have an id, so we update
 			if ($id) { 				
	 			// set the model id
	 			$this->id = $id;
			}
			
			// or create a new record
			else {
	 			$this->create();
			}
 			
			// see what we have
			//print_r($data);exit;
			//print_r($data);
 			// validate the row
			$this->set($data);
			//print_r($data);exit;
			$this->save($data);
            /*            
            if (!$this->validates()) {
				//$this->_flash('warning');
				$return['errors'][] = __(sprintf('Codetail for Row %d failed to validate.',$i), true);
				$error=true;
			}
			
 			// save the row
			if (!$error && !$this->save($data)) {
				$return['errors'][] = __(sprintf('Codetail for Row %d failed to save.',$i), true);
				$error=true;
			}

 			// success message!
			if (!$error) {
				$return['messages'][] = __(sprintf('Codetail for Row %d was saved.',$i), true);
			}
			//if($i==10){echo"sahabj"; break;}
              */            
                         
 		}
 		echo $i;
 		// close the file
 		fclose($handle);
 		
 		// return the messages
 		return $return;
 		
	}
	function CompanynameChanges($ComID,$CodeY)
	{
		if(!empty($CodeY)){
			$this->recursive=-1;
			$codetails = $this->find(
                'count',array("conditions"=>array("Codetail.Exch <>"=>"z","Codetail.CodeY"=>$CodeY,"Codetail.Cat"=>"d","Codetail.ComID !="=>$ComID))
                );
			//echo $codetails;
			if($codetails>0)
			return true;
		}
		else
		return false;
		
	}
}
